package com.wecloud.core.action.bussiness;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jasig.cas.client.util.CommonUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.wecloud.core.action.impl.BaseTransCfmAction;
import com.wecloud.core.constant.CorePathConfig;
import com.wecloud.core.constant.ErrorMsg;
import com.wecloud.core.constant.WeContext;
import com.wecloud.core.data.jnl.JnlService;
import com.wecloud.core.exception.WeException;
import com.wecloud.core.user.WeUser;
import com.wecloud.core.utils.RegexValidateUtil;
import com.wecloud.redis.service.RedisService;

/**
 * 更新用户信息服务
 * @author qq374
 *
 */
public class UpdateInfoAction extends BaseTransCfmAction{
	@Autowired
	private JnlService jnlService;//mysql
	@Autowired
	private RedisService redisService;//redis
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public void excute(WeContext context)  throws WeException{
		String nickName = context.getString("nickName");
		String telphone = context.getString("telphone");
		String email = context.getString("email");
		if(CommonUtils.isNotEmpty(email) && !RegexValidateUtil.checkEmail(email)){
			throw new WeException(ErrorMsg.Error_0007);
		}
		if(CommonUtils.isNotEmpty(telphone) && !RegexValidateUtil.checkEmail(telphone)){
			throw new WeException(ErrorMsg.Error_0008);
		}
		//检查手机号码是否已注册
		Map map = new HashMap();
		if(telphone != null && !"".equals(telphone)){
			map.put("telphone", telphone);
			context.setData("telphone", telphone);
			Map resultMap1 = jnlService.queryForObject("user.queryUserByPhone", map);
			if(resultMap1 != null){
				throw new WeException(ErrorMsg.Error_0010);
			}
		}
		if(email != null && !"".equals(email)){
			//检查邮箱是否已注册
			map.put("email", email);
			context.setData("email", email);
			Map resultMap2 = jnlService.queryForObject("user.queryUserByEmail", map);
			if(resultMap2 != null){
				throw new WeException(ErrorMsg.Error_0011);
			}
		}
		if(nickName != null && !"".equals(nickName)){
			map.put("nickname", nickName);
			context.setData("nickname", nickName);
			Map resultMap3 = jnlService.queryForObject("user.queryUserByNickName", map);
			if(resultMap3 != null){
				throw new WeException(ErrorMsg.Error_0011);
			}
		}
		if(context.getData("List") != null){
			List list = (List)context.getData("List");
			if(list != null && list.size() > 0){
				Map file = (Map)list.get(0);
//				context.setData("path", CorePathConfig.TX_PAH_LOCAL+file.get("path")+file.get("filename"));
//				map.put("path", CorePathConfig.TX_PAH_LOCAL+file.get("path")+file.get("filename"));
				context.setData("path",  CorePathConfig.TX_PAH+file.get("path")+file.get("filename"));
				map.put("path", CorePathConfig.TX_PAH+file.get("path")+file.get("filename"));
			}
		}
		context.setData("content", map);
	}
	@Override
	public void after(WeContext context) throws WeException {
		super.after(context);
		//更新redis用户信息
		WeUser user = redisService.get(context.getString("JSESSIONID"));
		if(context.getString("path") != null){
			user.setPath(context.getString("path"));
		}
		if(context.getString("email") != null){
			user.setEmail(context.getString("email"));
		}
		if(context.getString("telphone") != null){
			user.setPhone(context.getString("telphone"));
		}
		redisService.add(context.getString("JSESSIONID"), user);
	}
}
